package com.naiterui.ehp.bs.cms.modules.agent.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.exception.BusinessExceptionCode;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.agent.entity.AgAgentArea;
import com.naiterui.ehp.bs.cms.modules.agent.entity.AgArea;
import com.naiterui.ehp.bs.cms.modules.agent.po.AreaPagePO;
import com.naiterui.ehp.bs.cms.modules.agent.po.AreaSavePO;
import com.naiterui.ehp.bs.cms.modules.agent.service.IAgentAreaService;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AreaDetailVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AreaItemVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.TreeDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yiren
 * @since 2022/2/10
 */
@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.AGENT_MANGEN})
@AllArgsConstructor
@RestController
@RequestMapping("/agent/area")
@PreAuthorize("@p.check('agent:area')")
public class AgentAreaController {
	private final IAgentAreaService agentAreaService;
	@SysLog("查询大区列表")
	@GetMapping("/list")
	@ApiOperation("查询大区列表")
	// @PreAuthorize("@p.check('agent:area:list')")
	public PageVO<AreaItemVO> list(PageParamsVO pageParamsVO, AreaPagePO pagePO) {
		return agentAreaService.queryPage(pageParamsVO, pagePO, AgAgentArea.AREA_LEV_REGION);
	}
	@SysLog("查询大区详情")
	@GetMapping("/get")
	@ApiOperation("查询大区详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "区域ID", dataType = "long", paramType = "query", required = true),
	})
	// @PreAuthorize("@p.check('agent:area:get')")
	public AreaDetailVO get(Long id) {
		return agentAreaService.getDetail(id);
	}

	@SysLog("新增/修改大区数据")
	@PostMapping("/save")
	@ApiOperation("新增/修改大区数据")
	// @PreAuthorize("@p.check('agent:area:save')")
	public void save(@RequestBody AreaSavePO savePO) throws BusinessException {
		log.info("编辑大区数据param:{}", JSONUtil.toJsonStr(savePO));
		if (StrUtil.hasEmpty(savePO.getAreaName())){
			throw new BusinessException(BusinessExceptionCode.PARAM_ERROR);
		}
		if (savePO.getAreaName().length() > 40){
			throw new BusinessException(BusinessExceptionCode.PARAM_ERROR, "大区名称最大长度为40");
		}
		agentAreaService.saveArea(savePO, AgArea.AREA_LEV_REGION);
	}

	@SysLog("查询区域树型数据")
	@GetMapping("/find/tree")
	@ApiOperation("查询区域树型数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "类型：1: 全部数据, 2: 查包含省区的数据", dataType = "long", paramType = "query", required = true),
	})
	public List<TreeDataVO> findTree(Integer type) {

		return agentAreaService.findTreeData(type);
	}

}
